package com.ruoyi.web.controller.video.controller;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SeriesInfo;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProgramInfo;
import com.ruoyi.system.service.IProgramInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 节目信息Controller
 *
 * @author ruoyi
 * @date 2021-03-19
 */
@Controller
@RequestMapping("/tvProgram")
public class ProgramInfoController extends BaseController {
    private String prefix = "video/program";

    @Autowired
    private IProgramInfoService programInfoService;


    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 查询节目信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProgramInfo programInfo) {
        startPage();
        List<ProgramInfo> list = programInfoService.selectProgramInfoList(programInfo);
        return getDataTable(list);
    }

    /**
     * 导出节目信息列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "节目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProgramInfo programInfo) {
        List<ProgramInfo> list = programInfoService.selectProgramInfoList(programInfo);
        ExcelUtil<ProgramInfo> util = new ExcelUtil<ProgramInfo>(ProgramInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 新增节目信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增节目信息
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProgramInfo programInfo) {
        SysUser user= ShiroUtils.getSysUser();
        programInfo.setCreateBy(user.getUserName());
        return toAjax(programInfoService.insertProgramInfo(programInfo));
    }

    /**
     * 修改节目信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        ProgramInfo programInfo = programInfoService.selectProgramInfoById(id);
        mmap.put("programInfo", programInfo);
        return prefix + "/edit";
    }

    /**
     * 修改节目信息
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProgramInfo programInfo) {
        return toAjax(programInfoService.updateProgramInfo(programInfo));
    }

    /**
     * 删除节目信息
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "节目信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(programInfoService.deleteProgramInfoByIds(ids));
    }


    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<ProgramInfo> util = new ExcelUtil<ProgramInfo>(ProgramInfo.class);
        return util.importTemplateExcel("节目数据");
    }

    /**
     * *导入数据
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<ProgramInfo> util = new ExcelUtil<ProgramInfo>(ProgramInfo.class);
        List<ProgramInfo> programInfoList = util.importExcel(file.getInputStream());
        String userName=ShiroUtils.getSysUser().getUserName();
        String message = programInfoService.importInfo(programInfoList,userName);
        return AjaxResult.success(message);
    }
}
